package com.pycredit.config;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Properties;

import com.pycredit.exception.BizException;

public class UserModelConfig {

	private static double[][] userModelAlpha;

	private static double[] userModelBeta;

	private static HashMap<String, String[]> values;

	private static HashMap<String, String[]> xValues;

	private static double[] ratingWeight;

	private static UserModelConfig modelConfig = null;

	public synchronized static UserModelConfig getInstance() {
		if (modelConfig == null) {
			modelConfig = new UserModelConfig();
			init();
		}
		return modelConfig;
	}

	private UserModelConfig() {

	}

	private static void init() {
		Properties props = new Properties();
		try {
			String fileName = "usermodel.properties";
			InputStream inputStream = null;
			try {
				inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); // properties.load(Prop.class.getResourceAsStream(fileName));
				if (inputStream == null)
					throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
				props = new Properties();
				props.load(new InputStreamReader(inputStream, "UTF-8"));
			} catch (IOException e) {
				throw new Exception("Error loading properties file.", e);
			}
			//props.load(ClassLoader.getSystemResourceAsStream("usermodel.properties"));

			// 读取beta值
			String betaStr = props.getProperty("beta");
			String[] betaStrArr = betaStr.split(";");
			userModelBeta = new double[betaStrArr.length];
			for (int i = 0; i < betaStrArr.length; i++) {
				userModelBeta[i] = Double.parseDouble(betaStrArr[i]);
			}

			// 读取alpha值
			String[] alphaSizeArr = props.getProperty("alpha.size").split(",");
			int alphai = Integer.parseInt(alphaSizeArr[0]);
			int alphaj = Integer.parseInt(alphaSizeArr[1]);
			userModelAlpha = new double[alphai][alphaj];
			for (int i = 0; i < alphai; i++) {
				String[] alphaArr = props.getProperty("alpha" + i).split(",");
				for (int j = 0; j < alphaj; j++) {
					userModelAlpha[i][j] = Double.parseDouble(alphaArr[j]);
				}
			}

			// 读取系数值
			Integer categorySize = Integer.parseInt(props.getProperty("catagory.size"));
			values = new HashMap<String, String[]>();
			xValues = new HashMap<String, String[]>();

			String vKey = null;
			String xvKey = null;
			for (int i = 1; i < categorySize + 1; i++) {
				vKey = "C" + i + "-values";
				xvKey = "C" + i + "-xValues";
				values.put("C" + i, String.valueOf(props.get(vKey)).split(";"));
				xValues.put("C" + i, String.valueOf(props.get(xvKey)).split(";"));
			}

			// 读取比重值
			String[] ratingWeightArr = props.getProperty("rating.weight").split(",");
			ratingWeight = new double[ratingWeightArr.length];
			for (int i = 0; i < ratingWeightArr.length; i++) {
				ratingWeight[i] = Double.parseDouble(ratingWeightArr[i]);
			}

			// verify 配置
			if (values.size() != xValues.size()) {
				throw new BizException("UA-0001", "参数配置不正确,请检查配置文件!");
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public double[][] getUserModelAlpha() {
		return userModelAlpha;
	}

	public double[] getUserModelBeta() {
		return userModelBeta;
	}

	public HashMap<String, String[]> getValues() {
		return values;
	}

	public HashMap<String, String[]> getxValues() {
		return xValues;
	}

	public double[] getRatingWeight() {
		return ratingWeight;
	}

	public static void main(String[] args) {
		UserModelConfig config = UserModelConfig.getInstance();
		System.out.println(config.getValues().get("C3")[1]);
	}

}
